import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../image/custom_image.dart';

class BuglyUpgradeWidget extends StatelessWidget {
  /// 对话框的宽度
  final double width;

  /// 版本号
  final String versionName;

  /// 更新时间
  final String updateTimestamp;

  /// 更新内容
  final String updateContent;

  /// 包信息字体
  final TextStyle packageInfoStyle;

  /// content容器字体
  final TextStyle contentTextTitleStyle;

  /// content容器字体
  final TextStyle contentTextStyle;

  /// 按钮文字的大小
  final TextStyle buttonTextStyle;

  /// 顶部图片
  final Widget? topImage;

  /// 边框圆角大小
  final double radius;

  /// 更新事件
  final VoidCallback onUpgradeClickListener;

  /// 进度条的背景颜色
  final Color progressBackgroundColor;

  /// 点击退出的事件
  final VoidCallback onCancelClickListener;

  /// 是否是强制更新
  final bool isForce;

  /// 更新按钮内容
  final String updateButtonText;

  /// 按钮背景颜色
  final Color buttonBgColor;

  BuglyUpgradeWidget({
    Key? key,
    this.width = 238,
    required this.versionName,
    required this.updateTimestamp,
    required this.updateContent,
    required this.onCancelClickListener,
    required this.onUpgradeClickListener,
    this.packageInfoStyle = const TextStyle(fontSize: 12, color: Color(0xFF757575)),
    this.contentTextTitleStyle = const TextStyle(fontSize: 12, color: Color(0xFF282828)),
    this.contentTextStyle = const TextStyle(fontSize: 12, color: Color(0xFF282828)),
    this.buttonTextStyle = const TextStyle(fontSize: 14.0, color: Colors.white, fontWeight: FontWeight.w500),
    this.buttonBgColor = Colors.red,
    this.progressBackgroundColor = const Color(0xFFFFCDD2),
    this.topImage,
    this.radius = 6,
    this.isForce = false,
    this.updateButtonText = '立即更新',
  }) : super(key: key);

  /// 构建包信息item
  Widget _buildPackageInfoItem(String s) {
    return SizedBox(
      height: 22,
      child: Text(s, maxLines: 1, style: packageInfoStyle),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          topImage ??
              CustomImage.asset(
                "assets/images/common/update_bg_app_top.png",
                fit: BoxFit.cover,
                width: width,
              ),
          Container(
            width: width,
            padding: const EdgeInsets.only(left: 16, right: 16, top: 14, bottom: 14),
            color: Colors.white,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildPackageInfoItem("版本: $versionName"),
                _buildPackageInfoItem("更新时间: $updateTimestamp"),
                Container(
                  margin: const EdgeInsets.only(top: 13, bottom: 10),
                  child: Text("更新说明: ", style: contentTextTitleStyle),
                ),
                Text(updateContent, style: contentTextStyle),
                FractionallySizedBox(
                  widthFactor: 1,
                  child: GestureDetector(
                    onTap: onUpgradeClickListener,
                    child: Container(
                      height: 34,
                      margin: const EdgeInsets.only(top: 12),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(color: buttonBgColor, borderRadius: BorderRadius.circular(radius)),
                      child: Text(updateButtonText, style: buttonTextStyle),
                    ),
                  ),
                ),
              ],
            ),
          ),
          if (!isForce)
            GestureDetector(
              onTap: onCancelClickListener,
              child: Container(
                margin: const EdgeInsets.only(top: 45),
                height: 25,
                width: 25,
                alignment: Alignment.center,
                child: CustomImage.asset("assets/images/common/update_ic_close.png"),
              ),
            ),
        ],
      ),
    );
  }
}
